﻿using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace zielonka.co.uk.extensions.system.windows.forms.button
{
    //Description:	Remove click event from given button.
    /*
        btnTest.RemoveClickEvent();
     */

    public static partial class ButtonExtensions
	{
        public static void RemoveClickEvent(this Button btn)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
            if (f1 != null)
            {
                object obj = f1.GetValue(btn);
                PropertyInfo pi = btn.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
                EventHandlerList list = (EventHandlerList)pi.GetValue(btn, null);
                list.RemoveHandler(obj, list[obj]);
            }
        }
	}
}
